package com.huawei.codearts.listener;

import com.huawei.codearts.tools.utils.EtcdUtils;
import io.etcd.jetcd.*;
import io.etcd.jetcd.watch.WatchEvent;
import io.etcd.jetcd.watch.WatchResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Scanner;

@Slf4j
public class EtcdWatchApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    private final Client etcdClient = EtcdUtils.etcdClient();

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        ApplicationContext parent = applicationContext.getParent();
        if (parent == null) {
//            CacheManager cacheManager = applicationContext.getBean(CacheManager.class);
//            Cache cache = cacheManager.getCache(MsConstants.NODE_CACHE_NAME);
//            Cache cache = cacheManager.getCache(Constants.NULL_KEY.toString());
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //代码
                    log.info("watch 开始监听..." + Thread.currentThread().getName());

                    Watch watchClient = etcdClient.getWatchClient();
                    ByteSequence key = ByteSequence.from("/config/application".getBytes());
                    try {
                        watchClient.watch(key, new Watch.Listener() {
                            @Override
                            public void onNext(WatchResponse response) {
                                for (WatchEvent event : response.getEvents()) {
                                    System.out.println("操作: " + event.getEventType());
                                    KeyValue kv = event.getKeyValue();
                                    System.out.println("当前kv: ");
                                    System.out.println(new String(kv.getKey().getBytes()));
                                    System.out.println(new String(kv.getValue().getBytes()));
                                    System.out.println(kv.getVersion());
                                    System.out.println(kv.getCreateRevision());
                                    System.out.println(kv.getModRevision());
                                    System.out.println(kv.getLease());
                                }
                            }

                            @Override
                            public void onError(Throwable throwable) {
                                log.info("停止监听... error: {}", throwable.getMessage());
                            }

                            @Override
                            public void onCompleted() {
                                log.info("结束监听...");
                            }
                        });
                        System.out.println("键盘输入结束监听!");
                        Scanner sc = new Scanner(System.in);
                        sc.nextLine();
                    } finally {
                        if (watchClient != null) {
                            watchClient.close();
                        }
                    }

                }
            }).start();
        }
    }
}
